// app.ts

import { applyLocSvs } from "./services/location";
import { pointUp } from "./services/orbit"
import { getUnionId, restoreAuth, checkLogin } from './services/login'


let upstreamLimit = 0
let loc = null;

App<IAppOption>({
  globalData: {

  },
  onLaunch() {
    // 展示本地存储能力
    const logs = wx.getStorageSync('logs') || []
    logs.unshift(Date.now())
    wx.setStorageSync('logs', logs)

    restoreAuth(this.globalData)

    applyLocSvs((result: any) => {
      loc = result
      // console.log("update loc:", result.longitude, result.latitude)
      let now = (new Date()).getTime()
      if (now - upstreamLimit < 10*1000){
        return
      } else {
        upstreamLimit = now
      }
      pointUp(loc)
    }, () => {
      console.log('提示用户授权')
      wx.showModal({
        title: '权限申请',
        content: '打开定位后台定位才能使用全部功能',
        showCancel: true,
        success: (res: any) => {
          if (res.confirm) {
            wx.openSetting({
              success: (res:any) => {
                let sett = res.authSetting
                if (sett['scope.userLocationBackground']) {
                  console.log('位置权限申请成功')
                } else {
                  console.log('位置权限申请未能申请')
                }
                
              }
            })
          }
        }
      })
    })
  },
})